/*
 * @author java_deng
 * @date 2025/3/11 21:12
 * @description
 */
package org.kaka.service.impl;

import org.kaka.dto.InstitutionDto;
import org.kaka.mapper.InstitutionMapper;
import org.kaka.utils.ResponseData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class InstitutionService {
    @Autowired
    private InstitutionMapper institutionMapper;

    public List<InstitutionDto> list(InstitutionDto institutionDto) {
        List<InstitutionDto> list = institutionMapper.list(institutionDto);
        list.forEach(item -> {
            Integer level = item.getLevel();

            if (level > 1) {
                Integer parentId = item.getParentId();
                if (parentId != null) {
                    item.setParentName(institutionMapper.selectById(item.getParentId()).getName());
                }
            }
            StringBuffer fullName = new StringBuffer(item.getName());
            InstitutionDto newIns=item;
            while (level > 1) {
                Integer itemParentId = newIns.getParentId();
                if (itemParentId != null) {
                    InstitutionDto parent = institutionMapper.selectById(itemParentId);
                    fullName.insert(0, parent.getName() + "/");
                    level = parent.getLevel();
                    newIns=parent;
                } else {
                    break;
                }
            }

            item.setFullName(fullName.toString());
        });
        return list;
    }

    public void save(InstitutionDto institutionDto) {
        Integer parentId = institutionDto.getParentId();
        if (parentId != null) {
            InstitutionDto parent = institutionMapper.selectById(parentId);
            institutionDto.setLevel(parent.getLevel() + 1);
        } else {
            institutionDto.setLevel(1);
        }
        institutionMapper.insert(institutionDto);
    }

    public void update(InstitutionDto institutionDto) {
        InstitutionDto old = institutionMapper.selectById(institutionDto.getId());
        Integer parentId = institutionDto.getParentId();
        if (parentId != null) {
            InstitutionDto parent = institutionMapper.selectById(parentId);
            institutionDto.setLevel(parent.getLevel() + 1);
        } else {
            institutionDto.setLevel(old.getLevel());
        }
        institutionMapper.updateById(institutionDto);
    }

    public void delete(Long id) {
        institutionMapper.deleteById(id);
    }

    public InstitutionDto detail(Long id) {
        InstitutionDto item = institutionMapper.selectById(id);
        Integer level = item.getLevel();
        if (level > 1) {
            Integer parentId = item.getParentId();
            if (parentId != null) {
                item.setParentName(institutionMapper.selectById(item.getParentId()).getName());
            }
        }
        StringBuffer fullName = new StringBuffer(item.getName());
        InstitutionDto newIns=item;
        while (level > 1) {
            Integer itemParentId = newIns.getParentId();
            if (itemParentId != null) {
                InstitutionDto parent = institutionMapper.selectById(itemParentId);
                fullName.insert(0, parent.getName() + "/");
                level = parent.getLevel();
                newIns=parent;
            } else {
                break;
            }

        }
        item.setFullName(fullName.toString());
        return item;
    }
}
